package demo03;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class C3P0UtilsTestDemo {
	public static void main(String[] args) throws SQLException {
//		第一种方法
//		queryFirst();
//		xml配置文件，直接识别
//		insert();
//		delete();
		query();
	}

	//插入
	public static void insert() throws SQLException{
		//1.获取连接
		Connection conn = C3P0Utils02.getConnection();
		//2.获取执行对象
		Statement st = conn.createStatement();
		//3.执行sql
		int rows = st.executeUpdate("insert into users (username,password) values ('王八','123321')");
		System.out.println(rows);
		//4.
		C3P0Utils02.closeAll(conn, st, null);
	}
	//删除
	public static void delete() throws SQLException{
		//1.获取连接
		Connection conn = C3P0Utils02.getConnection();
		//2.获取执行对象
		Statement st = conn.createStatement();
		//3.执行sql
		int rows = st.executeUpdate("delete from users where uid = 7");
		System.out.println(rows);
		//4.
		C3P0Utils02.closeAll(conn, st, null);
	}
	//查询
	public static void query() throws SQLException{
		//1.获取连接
		Connection conn = C3P0Utils02.getConnection();
		//2.获取执行对象
		Statement st = conn.createStatement();
		//3.执行sql
		ResultSet rs = st.executeQuery("select * from users");
		while(rs.next()){
			System.out.println(rs.getObject("uid")+"\t"+rs.getObject("username")+"\t"+rs.getObject("password"));
		}
		//4.
		C3P0Utils02.closeAll(conn, st, null);
	}
	//查询
	public static void queryFirst() throws SQLException{
		//1.获取连接
		Connection conn = C3P0Utils.getConnection();
		//2.获取执行对象
		Statement st = conn.createStatement();
		//3.执行sql
		ResultSet rs = st.executeQuery("select * from users");
		while(rs.next()){
			System.out.println(rs.getObject("uid")+"\t"+rs.getObject("username")+"\t"+rs.getObject("password"));
		}
		//4.
		C3P0Utils02.closeAll(conn, st, null);
	}

}
